package cn.edu.tju.se.auth.controller;

import cn.edu.tju.se.auth.enity.Customer;
import cn.edu.tju.se.auth.enity.Orders;
import cn.edu.tju.se.auth.service.CustomService;
import cn.edu.tju.se.auth.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "customer")
public class CustomController {

    @Autowired
    private CustomService customService;

    @Autowired
    private OrdersService ordersService;

    @DeleteMapping(path = "deleteCustomer")
    public void deleteCustomer(@RequestBody Customer customer){
        //注销用户
        customService.deleteCustomer(customer);
    }

    @PostMapping(path = "updateCustomer")
    public void updateCustomer(@RequestBody Customer customer){
        //修改用户信息（用id）
        customService.addCustomer(customer);
    }

    @GetMapping(path = "printOrder")
    public List<?> getOrder(@RequestParam String username){
        //用户查看订单
        List<List<?>> res = new ArrayList<>();
        res.add(ordersService.findOrderByUserName(username));
        return res;
    }

    @PostMapping(path = "updateOrder")
    public void updateOrder (@RequestBody Orders orders)
    {
        //用户修改订单
        //但有个问题，只要知道用户名就可以修改别人的，待改正
        //在考虑可不可以加一个password进行一定的隔离
        ordersService.addOrders(orders);
    }

    @GetMapping(path = "getCustomer")
    public List<?> getCustomer(@RequestParam String username){
        //用户查看个人信息
        return customService.findByusername(username);
    }

//按理说客户是不能拥有查看所有用户的权利的，因此在这里注掉他，此段代码仅为了方便测试时使用
//    @GetMapping(path = "getAllCustomers")
//    public List<?> getAllCustomers(){
//        return customService.getAllCustomers();

    @PostMapping(path = "addCustomer")
    public void addCustomer(@RequestBody Customer customer){
        customService.addCustomer(customer);
    }

//
//    @DeleteMapping(path = "deleteCustomer")
//    public void deleteCustomer(@RequestBody Customer customer){
//        customRepository.delete(customer);
//    }

}
